package cn.woniu.ecommerceplatform.service.impl;

import cn.woniu.ecommerceplatform.redis.RedisLabel;
import cn.woniu.ecommerceplatform.util.ResponseResult;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.woniu.ecommerceplatform.entity.Label;
import cn.woniu.ecommerceplatform.service.LabelService;
import cn.woniu.ecommerceplatform.mapper.LabelMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
* @author admin
* @description 针对表【t_label】的数据库操作Service实现
* @createDate 2023-01-05 19:40:20
*/
@Service
public class LabelServiceImpl extends ServiceImpl<LabelMapper, Label>
    implements LabelService{
    @Resource
    private LabelMapper labelMapper;

    @Autowired(required = false)
    private RedisLabel redisLabel;

    @Override
    public ResponseResult<?> queryList(String labelName, Integer pageNum, Integer pageSize) {
        List<Label> labelList ;
        PageInfo<Label> labelPageInfo;
        if(pageNum!=null && pageSize!=null){
            PageHelper.startPage(pageNum,pageSize);
            labelList = labelMapper.queryList(labelName);
            labelPageInfo = new PageInfo<>(labelList);
            if (!(labelPageInfo.getSize()>0)&&pageNum>1){
                PageHelper.startPage(pageNum-1,pageSize);
                labelPageInfo = new PageInfo<>(labelList);
            }
        } else {
            labelList = labelMapper.queryList(labelName);
            labelPageInfo = new PageInfo<>(labelList);
        }

        if (labelList.isEmpty()){
            return new ResponseResult<>().notok();
        }
        return new ResponseResult<>().ok(labelPageInfo);
    }

    @Override
    public ResponseResult<?> deleteById(Integer id) {
        int i = labelMapper.deleteById(id);
        if (i>0){
            return new ResponseResult<>().ok();
        } else {
            return new ResponseResult<>().notok();
        }
    }

    @Override
    public ResponseResult<?> deleteList(Integer[] ids) {
        int i = labelMapper.deleteBatchIds(Arrays.asList(ids));
        if (i>0){
            return new ResponseResult<>().ok();
        } else {
            return new ResponseResult<>().notok();
        }
    }

    @Override
    public ResponseResult<?> insertLabel(Label label) {
        int i = labelMapper.insert(label);
        if (i>0){
            return new ResponseResult<>().ok();
        } else {
            return new ResponseResult<>().notok();
        }
    }

    @Override
    public ResponseResult<?> updateLabelById(Label label) {
        int i = labelMapper.updateById(label);
        if (i>0){
            return new ResponseResult<>().ok();
        } else {
            return new ResponseResult<>().notok();
        }
    }
}




